App.controller("addUnionOrderActivityCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	var params = JSON.parse($stateParams.params);
	console.log("路由参数：",params);
	
	var limitParam = {imageSize:100, format:"jpg"};		//封面图
	var editor = null;	//编辑器
	var gifNum = 0;     //记录礼品的个数
	var publish_status = "";//活动发布状态
	$scope.form = {};		//表单
	$scope.showsubmits = true;   //新增按钮的现实
	$('.add_reduce').hide();  //添加按钮出现
	
	var provinceCode1 = null;    //商户区域的省
	var cityCode1 = null;    //商户区域的市
	var areaCode1 = null;    //商户区域的区
	var showstoreData=[];  	 //展示的商户
	var smashEageData=[];    //砸金蛋
	var lotteryData =[];     //抽奖券
	var souvenir = [];//礼物的返回数据
	var isTrueSign = 0;  //是否有签到礼 （0表示没有，1表示有）
	var activityDatas = {};    //获取活动项内容的数据
	var fullId = 1;    //记录回显的时候有几个礼品
	var chooseBrandNameNums = [];   //参与商户的id集合
	$('.intervalLine .presentAtNow').attr("disabled","true");  //活动内容下的input 都不能用
	
	var limitParam = {imageSize:100, format:"jpg"};		//封面图
	
	//权限判断
	if(userInfo.user_level == 3){
		$scope.company = true;
		$scope.exclusive = false;
		if(params.method=="look"||params.method=="editpulish"){
			$scope.shopShow = true;
		}else{
			$scope.shopShow = false;
		}
	}if(userInfo.user_level == 2){
		$scope.company = false;
		$scope.exclusive = true;
		$scope.shopShow = true;
	}
	
	//获取活动项内容
	$scope.getActivityItems = function(id){
		var paramObject ={};
		var urls1 = "";
			if(params.method=="look"||params.method=="editpulish"){
				paramObject = {
						activity_id:id
				};
				urls1 =  path+"/server/selectActivityDetail";
			}else{
				paramObject = {
						p_id:id
				};
				urls1 =  path+"/server/selectTemplateDetail"
			}
		$http({
			   url:urls1,
			   method: 'POST',
			   data:angular.toJson(paramObject)
		}).success(function(data){
			console.log(data.result);
			activityDatas = data.result;
			$scope.activity_name = activityDatas.name;    //活动名称
			publicService.showUploadImage("cover_img","cover_url",activityDatas.imgurl);   //封面图
			$timeout(function(){
				editor.setContent(activityDatas.details);		//详情图
				if(params.method=="look"||params.method=="look2"){
					$("input").attr("readonly","readonly");
					$(".operate").hide();
					editor.setDisabled("fullscreen");
				}
		    }, 1000);
			$('#startTime_spread').val(activityDatas.generalize_start);                  //活动推广时间(开始)
			$('#endTime_spread').val(activityDatas.generalize_end);                  //活动推广时间(结束)
			$('#startTime').val(activityDatas.start_time);                  //活动时间(开始)
			$('#endTime').val(activityDatas.end_time);                  //活动时间（结束）
			var productCode = activityDatas.code.split('-');                           
			$("#province").data("kendoComboBox").value(productCode[0]);                  //省
			publicService.getCityData(1,productCode[0],productCode[1]);                                    //设置市的数据
			$timeout(function(){
				publicService.getAreaData(1,productCode[1],productCode[2]);
		    }, 100);
			$('#activityAddress').val(activityDatas.stage);                                //具体的地址  
			publicService.showUploadImage("activity_card_img","activity_card_url",activityDatas.blockurl);   //活动内容图
			
	    	//集团下的活动内容
			if(params.method=="look2"||params.method=="edit"){
	    		var activitys = activityDatas.buy_money;
	    		if(activitys){
	    			$scope.values_1 = true;
	    			$scope.textLeft1 =parseInt(activityDatas.buy_money);     //购买金额
			    	$scope.voucher =parseInt(activityDatas.mortgage_money);  //抵扣金额
			    	$scope.fullTo = parseInt(activityDatas.use_money);      //满减
			    	$scope.isOrNoCheack($scope.values_1,1);
	    		}
	    		
	    		var dataList = [];
				var values = []; 
	    		$.each(data.result.templateFiliale,function(index,oneObj){	//参与分公司
	    			var dataObj = {
							name: oneObj.name,
							id: oneObj.user_id
					};
					dataList.push(dataObj);
					values.push(dataObj.id);
				});
	    		var plugsObj = $("#joinCommercial").data("kendoMultiSelect");
				plugsObj.setDataSource(dataList);
				plugsObj.value(values);
	    	}else if(params.method=="look"||params.method=="editpulish"){
		    	publish_status =activityDatas.publish_status;  //发布的状态
		    	
		    	var dataList = [];
				var values = []; 
	    		$.each(data.result.activityShop,function(index,oneObj){	//参与分公司
	    			var dataObj = {
							name: oneObj.name,
							id: oneObj.user_id
					};
					dataList.push(dataObj);
					values.push(dataObj.id);
				});
	    		var plugsObj = $("#joinCommercial").data("kendoMultiSelect");
				plugsObj.setDataSource(dataList);
				plugsObj.value(values);
				
		    	//分公司下的活动内容
		    	$.each(data.result.activityContent,function(index,objs){  
		    		if(objs.itme_id ==1){      //活动卡
	//	    			$(".intervalLine input[type='checkbox']").eq(0).prop('checked',true);  //框选中
		    			$scope.values_1 = true;
				    	$scope.textLeft1 = objs.buy_money;     //购买金额
				    	$scope.voucher = objs.mortgage_money;  //抵扣金额
				    	$scope.fullTo =  objs.use_money;      //满减
				    	$scope.isOrNoCheack($scope.values_1,1);
				    	
		    		}else if(objs.itme_id ==2){   //签到
	//	    			$(".intervalLine input[type='checkbox']").eq(1).prop('checked',true);  //框选中
		    			$scope.values_2 = true;
		    			isTrueSign  =objs.sign;    //是否有签到礼
		    			if(userInfo.user_level == 3){
		    				$.each(objs.gifts,function(index,objs){
		    					objs.gift_id = objs.id;
		    				})
		    				$("#signInGif").data("kendoDropDownList").setDataSource(objs.gifts);  //签到理
		    			}
		    			$("#signInGif").data("kendoDropDownList").value(objs.gift);  //签到理
		    			$scope.isOrNoCheack($scope.values_2,2);
		    		}else if(objs.itme_id ==3){  //定金
	//	    			$(".intervalLine input[type='checkbox']").eq(2).prop('checked',true);  //框选中
		    			$scope.values_3 = true;
		    			$scope.payment = objs.use_money;         //定金
		    			$scope.isOrNoCheack($scope.values_3,3);
		    			
		    		}else if(objs.itme_id ==4){  //砸金蛋
	//	    			$(".intervalLine input[type='checkbox']").eq(3).prop('checked',true);  //框选中
		    			$scope.values_4 = true;
		    			$scope.smash = objs.use_money;         //定金额度
						$('#showEageGif').data("kendoMultiSelect").setDataSource(objs.gifts);    //砸金蛋
						$('#showEageGif').data("kendoMultiSelect").value(objs.gift.split(','));            //砸金蛋
						$scope.isOrNoCheack($scope.values_4,4);
		    		}else if(objs.itme_id ==5){  //抽奖券
	//	    			$(".intervalLine input[type='checkbox']").eq(4).prop('checked',true);  //框选中
		    			$scope.values_5 = true;
		    			$scope.looter = objs.use_money;                //定金额度
						$('#showlotteryGif').data("kendoMultiSelect").setDataSource(objs.gifts);    //砸金蛋
						$('#showlotteryGif').data("kendoMultiSelect").value(objs.gift.split(','));            //砸金蛋
						$scope.isOrNoCheack($scope.values_5,5);
		    		}else if(objs.itme_id ==6){
	//	    			$(".intervalLine input[type='checkbox']").eq(5).prop('checked',true);  //框选中
		    			$scope.values_6 = true;
		    			$scope.gifNum = objs.use_money;  //满额度
		    			if(userInfo.user_level == 3){
		    				$.each(objs.gifts,function(index,objs){
		    					objs.gift_id = objs.id;
		    				})
		    				$("#presents").data("kendoDropDownList").setDataSource(objs.gifts);  //签到理
		    			}
		    			$("#presents").data("kendoDropDownList").value(objs.gift);  //礼品
		    			$scope.isOrNoCheack($scope.values_6,6);
		    		}else if(objs.itme_id>=7){
		    			$scope.addgif();  
		    			$('#fullI'+fullId).val(objs.use_money);   //满额度
		    			if(userInfo.user_level == 3){
		    				$.each(objs.gifts,function(index,objs){
		    					objs.gift_id = objs.id;
		    				})
		    				$("#signInGif").data("kendoDropDownList").setDataSource(objs.gifts);  //签到理
		    			}
		    			$("#presents"+fullId).data("kendoDropDownList").value(objs.gift)  //礼品
		    			fullId  = ++fullId;
		    			if(params.method=="look"||params.method=="look2"){
		    				
		    			}
		    		}
		    	});
	    	}
		}).error(function(data){
			console.log(data);   
		})
	};
	
	//初始化图文编辑器
	$scope.initEditor = function(editorId){
		var option = {initialFrameHeight:400};

		if(UE.getEditor(editorId,option).container){
			UE.getEditor(editorId).destroy();
		}
		editor = UE.getEditor(editorId,option);
	}
	//初始化省
	$scope.initProvince = function(){
		$("#province1").kendoComboBox({
		    dataTextField: "name",
		    dataValueField: "code",
		    placeholder: "省",
		    filter: "contains",
		    dataSource: {
		      serverFiltering: true,
	          transport: {
	              read: {
	            	  type : 'GET',
	                  url: path+"/server/province",
	                  dataType : "json"
	              }
	          },
	          schema : {
					data : function(d) {
						return d.result;
					}
				},
	      },
	      change: function(e) {
	    	    var valueList = this.value();
	    	    provinceCode1 = valueList;
	    	    if(valueList&&valueList.length>0){
	    	    	$("#area1").data("kendoComboBox").value("");
	    	    }
	    	    var plugObj1 = $("#userShop").data("kendoDropDownList");
	    	    plugObj1.setDataSource("");
	    	    
	    	    $scope.getCityData(valueList);
	      }
		});
	}
	
	//获取市数据
	$scope.getCityData = function(provinceCode,defaultVal){
		$http({
			   url: path+"/server/city/"+provinceCode,
			   method: 'GET'
		}).success(function(data){
			$scope.initCity(data.result,defaultVal);
		}).error(function(data){
			console.log(data);   
		})
	};
	
	//初始化市
	$scope.initCity = function(cityData,defaultVal){
		if(cityData){
			var plugObj = $("#city1").data("kendoComboBox");
			plugObj.value("");
			plugObj.setDataSource(cityData);
			if(defaultVal){
				plugObj.value(defaultVal);
			}
		}else{
			$("#city1").kendoComboBox({
			    dataTextField: "name",
			    dataValueField: "code",
			    placeholder: "市",
			    filter: "contains",
			    dataSource: [],
			    change: function(e) {
		    	    var value = this.value();
		    	    cityCode1 = value;
		    	    if(value&&value.length>0){
		    	    	$("#area1").data("kendoComboBox").value("");
		    	    }
		    	    $scope.getAreaData(value);
		      }
			});
		}
	}
	
	//获取区数据
	$scope.getAreaData = function(cityCode,defaultVal){
		$http({
			   url: path+"/server/county/"+cityCode,
			   method: 'GET'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
		}).success(function(data){
			 areaArr = data.result;
			allAreaStr = '';
			$.each(areaArr, function(index, OneObj){
				allAreaStr += (OneObj.code+",");
			});
			if(areaArr.length == 0){  //没有区的时候传市的编码
				allAreaStr = cityCode;
			}
			var allArea = {code:"all",name:"全城"};  //全城
			areaArr.unshift(allArea);

			$scope.initArea(areaArr,defaultVal);
		}).error(function(data){
			console.log(data);   
		})
	};
	
	//初始化区
	$scope.initArea = function(areaData,defaultVal){
		if(areaData){
			var plugObj = $("#area1").data("kendoComboBox");
			plugObj.setDataSource(areaData);
			if(defaultVal){
				plugObj.value(defaultVal);
			}
		}else{
			$("#area1").kendoComboBox({
				  dataTextField: "name",
				  dataValueField: "code",
				  placeholder: "请选择区域",
				  filter: "contains",
				  dataSource: [],
				  change:function(e){
					  valueList = this.value();
					  $scope.getCommercialInfo(provinceCode1,cityCode1,valueList);
					  //获取参与商户的信息
					  
				  }
			});
		}
	};
	  //获取参与商户的信息
	$scope.getCommercialInfo = function(pcode,ccode,acode){
		var baseOrder = {
				region_code: acode=="all"?ccode:acode
		};
		var urls = "";
		if(userInfo.user_level == 3){  //集团
			urls =  path+"/server/selectFilialeByCode";
		}if(userInfo.user_level == 2){  //分公司
			urls = path+"/server/selectShopByCode";
		}
		$http({
			   url:urls ,
			   method: 'POST',
			   data:baseOrder
		}).success(function(data){
			var dataList = [];
			var values = []; 
			$.each(data.result,function(index,oneObj){
				if(userInfo.user_level == 3){ //集团
					var dataObj = {
							name: oneObj.filialeName,
							id: oneObj.filialeId
					};
				}else if(userInfo.user_level == 2){//分公司
					var dataObj = {
							name: oneObj.shopName,
							id: oneObj.shopId
					};
				}
				dataList.push(dataObj);
				values.push(dataObj.id);
			});
			
			$("#userShop").data("kendoDropDownList").setDataSource(dataList);
			if(acode=="all"){
				var plugObj = $("#joinCommercial").data("kendoMultiSelect");
		    	var plugObj_dataSource = plugObj.options.dataSource;
		    	var plugObj_values = plugObj.value();
		    	
		    	$.each(dataList,function(index,oneObj){
		    		if(!publicService.isExitInArray(plugObj_dataSource,"id",oneObj)){
		    			plugObj_dataSource.push(oneObj);
		    		}
				});
		    	$.each(values,function(index,oneObj){
		    		if(!publicService.isExitInArray(plugObj_values,null,oneObj)){
		    			plugObj_values.push(oneObj);
		    		}
				});
		    	
		    	plugObj.setDataSource(plugObj_dataSource);
		    	plugObj.value(plugObj_values);
			}
		}).error(function(data){
			console.log(data);   
		})
	};
	//获取礼物的数据
	$scope.getGifData = function(){
		$http({
			   url: path+"/server/selectNewGiftActivityList",
			   method: 'POST'
		}).success(function(data){
			souvenir = data.result;
			var objes ={
					gift_id:'',
					name:'请选择'
			};
			souvenir.unshift(objes);
			$("#signInGif").data("kendoDropDownList").setDataSource(data.result);  //签到礼
			$("#smashEageGif").data("kendoDropDownList").setDataSource(data.result);  //砸金蛋
			$("#lotteryGif").data("kendoDropDownList").setDataSource(data.result);  //抽奖券
			$("#presents").data("kendoDropDownList").setDataSource(data.result);  //礼物
		}).error(function(data){
			console.log(data);   
		})
	};
	//-----------------------------------------各种实例化------------------------------------
//参与商户的初始化（选择）
	$scope.selecttComercial = function(){
		$("#userShop").kendoDropDownList({
		    dataTextField: "name",
		    dataValueField: "id",
		    placeholder:"请选择商户",
		    dataSource: [],
		    change: function(e) {
		    	var chooseObj = this.dataItem();
		    	var chooseValue = chooseObj.id;
		    	
		    	var plugObj = $("#joinCommercial").data("kendoMultiSelect");
		    	var plugObj_dataSource = plugObj.options.dataSource;
		    	var plugObj_values = plugObj.value();
		    	
		    	if(!plugObj_values.includes(chooseValue)){
		    		plugObj_values.push(chooseValue);
		    		if(!publicService.isExitInArray(plugObj_dataSource,"id",chooseValue)){
		    			plugObj_dataSource.push(chooseObj);
		    			plugObj.setDataSource(plugObj_dataSource);
		    		}
		    		plugObj.value(plugObj_values);
		    	}
	      	},
		});
	};
	
//选中的礼物展示实例化
	$scope.showGif = function(contains,name,Id){
		$("#"+contains).kendoMultiSelect({
		    placeholder: "暂未有礼品选中",
		    dataSource: [],
			dataTextField:name,
		    dataValueField:Id,
	      	change: function(e) {
	      	},
		});
	};
	
//礼物实例化 
	$scope.getGif = function(containId,presentData,showGif){
		$("#"+containId).kendoDropDownList({
		    placeholder:"请礼物",
			dataTextField: "name",
		    dataValueField: "gift_id",
		    dataSource: [],
		    change: function(e) {
		    	var value = this.value();
		    	var plugObj = $('#'+showGif).data("kendoMultiSelect");
		    	if(plugObj){
		    		var plugData = plugObj.dataItems();
		    	}
		    	//判断是不是签到礼是的话就只传 选中的name
		    	var present={
			    		"name":this.dataItem().name,
			    		"id":this.dataItem().gift_id
			    	};
				if(showGif&&this.dataItem().gift_id){
					if(!publicService.isExitInArray(plugData,'gift_id',value)){
						presentData.push(this.dataItem().gift_id);
						plugData.push(present);
						
					}
					$('#'+showGif).data("kendoMultiSelect").setDataSource(plugData); 
					$('#'+showGif).data("kendoMultiSelect").value(presentData); 
				}
				console.log(presentData);

	      	},
	      	select: function(e) {
	      	}
		});
	};

	//新增礼品
	$scope.addgif = function(){
		if(gifNum<=6){
			gifNum = ++gifNum;    //每次点击增加一个
			var html1 = '<div class="form-horizontal form-group gif'+gifNum+'">'
							+'<div class="col-sm-4">'
								+'<label class="col-sm-2 control-label">满</label>'
								+'<div class="col-sm-6">'
						    		+'<input type="text" class="form-control presentAtNow fullI"  id="fullI'+gifNum+'"/>'
						        +'</div>'
					    		+'<label class="col-sm-4 control-label textLeft">元获取</label>'
							+'</div>'
						    +'<div class="col-sm-4">'
								+'<input type="text" class="form-control presentAtNow presents"  id="presents'+gifNum+'"/>'
						   + '</div>'
						    +'<div class="col-sm-1">'
								+'<img class="add_reduce" src="../../images/icon/reduce.png" ng-click="removergif('+gifNum+')">'
						    +'</div>'
						+'</div>';
			html1 = $compile(html1)($scope);  
			$(html1).appendTo($('#addgiftBox'));
			$scope.getGif('presents'+gifNum+'');            //礼物
			$("#presents"+gifNum).data("kendoDropDownList").setDataSource(souvenir);  //礼物
			//souvenirAll.push(gifNum);
		}else{
			alert("最多可以增加8个礼品！");
		}
	};

	//删除增加的礼品
	$scope.removergif = function(id){
		$('#addgiftBox .gif'+id+'').remove();
		gifNum = --gifNum;    //每次点击增加一个
	};
	//判断勾选了以后是否填写内容

	//判断活动内容是否勾选
	$scope.isOrNoCheack = function(values,id){
		if(values){
			$('.aboutInput_'+id+' .presentAtNow').removeAttr("disabled");//让所有的input变为只读
//			$('.aboutInput_'+id+' .presentAtNow').val('');
			
			if(id ==2){
				$("#signInGif").data("kendoDropDownList").readonly(false);
			}else if(id ==4){
				$("#smashEageGif").data("kendoDropDownList").readonly(false);
			}else if(id ==5){
				$("#lotteryGif").data("kendoDropDownList").readonly(false);
			}else if(id ==6){
				$("#presents").data("kendoDropDownList").readonly(false);	
				$('.add_reduce').show();  //添加按钮出现
			}
		}else{
			$('.aboutInput_'+id+' .presentAtNow').attr("disabled","true");//让所有的input变为只读
			if(id ==1){  
				$scope.textLeft1 = "";
				$scope.voucher = "";
				$scope.fullTo = "";
			}else if(id ==2){
				$("#signInGif").data("kendoDropDownList").readonly(true);
			}else if(id ==3){
				$scope.payment = "";
			}else if(id ==4){
				$("#smashEageGif").data("kendoDropDownList").readonly(true);
				$scope.smash = "";
			}else if(id ==5){
				$("#lotteryGif").data("kendoDropDownList").readonly(true);
				 $scope.looter = "";
			}else if(id ==6){
				$("#presents").data("kendoDropDownList").readonly(true);
				$('.add_reduce').hide();  //添加按钮出现
			}
		}
		
	};
	//表单提交
	$scope.formSubmit = function(){
		showstoreData = $("#joinCommercial").data("kendoMultiSelect").value();
		var content=[];
		if(!$scope.activity_name){
			alert('请填写活动名称！');
			return;
		};
		if(!$('#cover_url').val()){
			alert("请填写封面图！");
			return;
		};
		if(!editor.getContent()){
			alert("请填写宣传图文！");
			return;
		};
		if(!$('#startTime_spread').val()){
			alert("请填写推广的开始时间！");	
			return;
		};
		if(!$('#endTime_spread').val()){
			alert("请填写推广的结束时间！");	
			return;
		};
		if(!$('#startTime').val()){
			alert("请填写活动开始时间！");
			return;
		};	
		if(!$('#endTime').val()){
			alert("请填写活动结束时间！");
			return;
		};	
		if(showstoreData.length<=0){
			alert('请选择参与的商户！');
			return;
		};
		if(!$('#area').val()){
			alert("请选择活动的地址！");
			return;
		};
		if(!$('#activityAddress').val()){
			alert("请选择填写的地址！");	
			return;
		};
		if(userInfo.user_level == 3&&!$('#activity_card_url').val()){
			alert("请选择活动内容的图片！");	
			return;
		};
		var myDate = new Date().getTime(); //获取当前时间
		var activity_start = new Date($('#startTime').val()).getTime();  //将活动开始时间转换成秒
		var activity_end = new Date($('#endTime').val()).getTime();  //将活动开始时间转换成秒
		var spread_start = new Date($('#startTime_spread').val()).getTime();  //将推广开始时间转换成秒
		var spread_end = new Date($('#endTime_spread').val()).getTime();  //将推广结束时间转换成秒
		if(myDate>activity_start){
			alert('活动开始时间需要大于当前时间！');
			return;
		}
		if(spread_start>activity_start){
			alert('活动开始时间需要大于推广时间！');
			return;
		}
		var decideTime1=  publicService.decideTime($('#startTime').val(),$('#endTime').val());   //判断结束时间大于开始时间
		var decideTime2=  publicService.decideTime($('#startTime_spread').val(),$('#endTime_spread').val());   //判断结束时间大于开始时间
		if(decideTime1){
			alert("活动开始时间应该大于结束时间！");
			return;
		};
		if(decideTime2){
			alert("推广开始时间应该大于结束时间！");
			return;
		};
		
		
		if($scope.values_1){  //判断活动内容项
			if(!$scope.textLeft1){
				alert('请完善活动卡的【购买金额】！');
				return;
			}else if($scope.textLeft1<=0){
				alert('活动卡的【购买金额】值不规范，请重新填写！');
				return;
			}
			
				if($scope.fullTo<0){
					alert('活动卡的【满减条件】值不规范，请重新填写！');
					return;
				}
			
				if($scope.voucher<0){
					alert('活动卡的【抵扣条件】值不规范，请重新填写！');
					return;
				}
			
		};
		if($scope.values_2){  //签到礼
			if($("#signInGif").data("kendoDropDownList").value()){
				
			}else{
				alert('勾选了签到礼，请填写内容！');
				return;
			}
		};
		if($scope.values_3){  //预付定金
			if($scope.payment){
				
			}else{
				alert('勾选了预付定金，请填写内容！');
				return;
			}
		};
		if($scope.values_4){  //砸金蛋
			if($scope.smash||$('#showEageGif').data("kendoMultiSelect").value()){
				if($scope.smash&&$('#showEageGif').data("kendoMultiSelect").value()){
					
				}else{
					alert('请将砸金蛋的内容填写完整！');
					return;
				}
			}else{
				alert('勾选了砸金蛋，请填写内容！');
				return;
			}
		};
		if($scope.values_5){  //抽奖券
			if($scope.looter||$('#showlotteryGif').data("kendoMultiSelect").value()){
				if($scope.looter&&$('#showlotteryGif').data("kendoMultiSelect").value()){
					
				}else{
					alert('请将抽奖券的内容填写完整！');
					return;
				}
			}else{
				alert('勾选了抽奖券，请填写内容！');
				return;
			}
		};
		if($scope.values_6){  //礼品
			if($scope.gifNum||$("#presents").data("kendoDropDownList").value()){
				if($scope.gifNum&&$("#presents").data("kendoDropDownList").value()){
					
				}else{
					alert('请将礼品的内容填写完整！');
					return;
				}
			}else{
				alert('勾选了礼品，请填写内容！');
				return;
			}
		};
		var codeALL =($('#province').val()+'-'+$('#city').val()+'-'+$('#area').val()).toString();  //拼接省市区的参数
		//基础内容
		$scope.form = {
		        "name":$scope.activity_name,
		        "imgurl":$('#cover_url').val(),
		        "start_time":$('#startTime').val(), //活动时间开始
		        "end_time":$('#endTime').val(), //活动时间结束
				"stage":$('#activityAddress').val(),            //活动地址
				"code":codeALL,         // 区域编码
				"details":editor.getContent(),		//宣文图文
				"generalize_start":$('#startTime_spread').val(),  //推广时间开始
				"generalize_end":$('#endTime_spread').val(),		//推广时间结束
				"blockurl":	$('#activity_card_url').val(),	//卡图	
				"publish_status":publish_status
		}
		//活动内容
		if($('#signInGif').val()){
			isTrueSign =1;
		}
		$.each($('.activityItem'),function(index,objs){
			var Object = {};
			$scope.form.buy_money = $scope.textLeft1;  //购买金额
			$scope.form.mortgage_money =$scope.voucher ; //抵用金额
			$scope.form.use_money =$scope.fullTo ;  //使用金额
			if(objs.id == 'activityCarsd'&&$scope.values_1){    //活动卡
				Object ={
			          "itme_id": 1 , //代表会员卡
			          "buy_money":$scope.textLeft1,   //购买金额
			          "mortgage_money":$scope.voucher , //抵扣金额
			          "use_money":$scope.fullTo		//满多少   使用金额	
				};
				content.push(Object);

			}else if(objs.id == 'activitygifts'&&$scope.values_2){   //签到礼
				Object ={
				          "itme_id": 2, // 签到礼
				          "sign":isTrueSign,
				          "gift":$("#signInGif").data("kendoDropDownList").value(),//礼品	
				};
				content.push(Object);
			}else if(objs.id == 'acitivityEarnest'&&$scope.values_3){   // 定金
				Object ={
			              "itme_id": 3,   //定金
			              "use_money":$scope.payment,  //定金额度	
				};
				content.push(Object);
			}else if(objs.id == 'eggSmash'&&$scope.values_4){    //砸金蛋
				Object ={
			              "itme_id": 4,//砸金蛋 
			              "use_money":$scope.smash,  //满额度
			              "gift":$('#showEageGif').data("kendoMultiSelect").value().toString()  //礼物		
				};
				content.push(Object);
			}else if(objs.id == 'drawCircle'&&$scope.values_5){   //抽奖券
				Object ={
			             "itme_id": 5,  //抽奖券
			             "use_money": $scope.looter,  //定金额度
			             "gift":$('#showlotteryGif').data("kendoMultiSelect").value().toString()       //礼品		
				};
				content.push(Object);
			}else if(objs.id == 'acitvitygifts'&&$scope.values_6){   //礼物
				var giftArr = $("#addgiftBox").children("div");
				$.each(giftArr,function(index,obj){
					var value = $(obj).children("div:nth(0)").children("div").children("input").val(); //满额度
					var value2 = $(obj).children("div:nth(1)").children('span').children('input').val();  //礼品
					var object1 ={};
					object1.itme_id = index+6; //礼品
					object1.use_money = value; //满额度
					object1.gift = value2;
					content.push(object1);
				});
			}
		});

		//参与商户信息
		var baseShopes =$("#joinCommercial").data("kendoMultiSelect").value();
		var baseShopeData = [];
//		$.each(baseShopes,function(index,objs){
//			var abaseshop = {};
//			abaseshop.user_id = objs;
//			baseShopeData.push(abaseshop);
//		})
		if(userInfo.user_level == 3){
			$scope.form.filialeIds = baseShopes;     //分公司id
		}if(userInfo.user_level == 2){
			$scope.form.shopIds = baseShopes;     //店铺的id
		}

		$scope.form.content = content;   //办理活动内容的值
		var postUrl = '';
		if(params.method=="add"){
			if(userInfo.user_level == 3){
				postUrl = path+'/server/addActivityTemplate';
			}if(userInfo.user_level == 2){
				postUrl = path+'/server/addActivity';
			}
		}else if(params.method=="edit"){
			$scope.form.p_id= params.brandId;
			postUrl = path+'/server/updateActivityTemplate';
		}else if(params.method=="editpulish"){
			$scope.form.activity_id= params.brandId;
			postUrl = path+'/server/updateActivity1';			
		}
		$http({
			   url:postUrl,
			   method: 'POST',
			   data:$scope.form
		}).success(function(data){
			if(data.code = '0'){
				alert('提交成功！');
				if(params.backUrl){
					$location.path("/"+params.backUrl);
				}else{
					window.location.reload();   //页面刷新
				}
			}else if(data.code = '1'){
				alert('接口出错！请联系管理员！');
			}
		}).error(function(data){
			console.warn(data);   
		})
	};
	
	//根据code获取name
	$scope.getNameByCode = function(code){
		$http({
			   url: path+'/server/getname/'+code,
			   method: 'GET' 
		}).success(function(data){
			var result = data.result;
			$("#province").data("kendoComboBox").value(result.province.code);
			$scope.getCityData(result.province.code,result.city.code);
			
			if(params.method!=="add"){
				//是否是全城
				if(sumData.isglobal){
					$scope.getAreaData(result.city.code,'all');
				}else{
					$scope.getAreaData(result.city.code,sumData.region_code.split(","));
				}
			}else{
				$scope.getAreaData(result.city.code,result.area.code.split(","));
				$scope.getArea();
			}

		}).error(function(data){
			alert(data);   
		})
	}	
	
	//显示默认的省份
	$scope.showDefaultProvince = function(){
		var provinceCode = userInfo.userInfoEntity.region_code.split(",")[0];
		$("#province1").data("kendoComboBox").value(provinceCode);
		$('#province1').data("kendoComboBox").readonly(true);
		$("#province").data("kendoComboBox").value(provinceCode);
		//$('#province').data("kendoComboBox").readonly(true);//panminghua 未完善，应该是默认当前的地址。
		$scope.getCityData(provinceCode);
		publicService.getCityData(1,provinceCode);
	}
	
	
	$scope.getGifData();//请求礼物的数据
	
	publicService.uploadImage("cover","cover_img","cover_url",limitParam);
	$scope.initEditor("editor");	//初始化编辑器
	publicService.uploadImage("activity_card","activity_card_img","activity_card_url",limitParam);
	publicService.initDateTime("startTime_spread");		//推广时间（始）
	publicService.initDateTime("endTime_spread");		//推广时间（止）
	publicService.initDateTime("startTime");	//开始时间
	publicService.initDateTime("endTime");		//结束时间
	
//	publicService.initMultiSelect("joinCommercial");	//初始化用户【选中】
	$scope.selecttComercial();	//初始化用户【展示】
	publicService.initProvince(1);		//初始化省
	publicService.initCity(1);			//初始化市
	publicService.initArea(1);			//初始化区
	$scope.initProvince();			//初始化省（参与商户）
	$scope.initCity();				//初始化市（参与商户）
	$scope.initArea();				//初始化区（参与商户）
	
	
	$scope.getGif('signInGif');            //礼物实例化(签到礼)
	$scope.getGif('smashEageGif',smashEageData,'showEageGif');     //礼物实例化(砸金蛋)
	$scope.getGif('lotteryGif',lotteryData,'showlotteryGif');         //礼物实例化(抽奖券)
	$scope.getGif('presents');            //礼物
	$scope.showGif('showEageGif','name','id');//砸金蛋
	$scope.showGif('showlotteryGif','name','id');//抽奖券
	$scope.showGif("joinCommercial",'name','id');    //选中参与商户的展示
	
	if(params.method=="edit"||params.method=="editpulish"){   //编辑
		$('.acitvityshowAndHide').hide();
		$scope.getActivityItems(params.brandId);		//获取活动内容项
		$scope.showsubmits = true;   //新增按钮的现实
		$timeout(function(){
			chooseBrandNameNums =  $("#joinCommercial").data("kendoMultiSelect").value();
	    }, 1000);
		if(params.method=="editpulish"){  //推广时间和活动时间不能更改
//			$("#startTime_spread").data("kendoDateTimePicker").readonly(true);
//			$("#endTime_spread").data("kendoDateTimePicker").readonly(true);
//			$("#startTime").data("kendoDateTimePicker").readonly(true);
//			$("#endTime").data("kendoDateTimePicker").readonly(true);
			
			$scope.showDefaultProvince();
		}
	}else if(params.method=="look"||params.method=="look2"){//查看
		$scope.showsubmits = false;   //新增按钮的现实
		$('.acitvityshowAndHide').hide();
		$scope.getActivityItems(params.brandId);		//获取活动内容项	
		$("input").attr("readonly","readonly");//让所有的input变为只读
		$("input").attr("disabled","disabled");
		$("textarea").attr("readonly","readonly");
		$("#signInGif").data("kendoDropDownList").readonly(true);
		$("#presents").data("kendoDropDownList").readonly(true);
		$("#smashEageGif").data("kendoDropDownList").readonly(true);
		$("#lotteryGif").data("kendoDropDownList").readonly(true);
		
		$("#showlotteryGif").data("kendoMultiSelect").readonly(true);
		$("#showEageGif").data("kendoMultiSelect").readonly(true);
	}else if(params.method=="add"){	
		if(userInfo.user_level == 3){	//集团创建模板
			
		}if(userInfo.user_level == 2){	//分公创建活动
			$scope.showDefaultProvince();
		}
	}
	
	
	
});